﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Quartz.WebManager.Model
{
    /// <summary>
    /// Defines a trigger that schedules a specific job instance.
    /// </summary>
    public class Trigger
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Trigger"/> class.
        /// </summary>
        public Trigger()
            : this(null)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="Trigger"/> class based on an other one.
        /// </summary>
        /// <param name="copy">The reference.</param>
        public Trigger(Trigger copy)
        {
            if (copy == null)
            {
                this.MisfireInstruction = Quartz.MisfireInstruction.SmartPolicy;
                this.Priority = 5;
            }
            else
            {
                this.Group = copy.Group;
                this.Name = copy.Name;
                this.Description = copy.Description;
                this.JobGroup = copy.JobGroup;
                this.JobName = copy.JobName;
                this.StartTime = copy.StartTime;
                this.EndTime = copy.EndTime;
                this.Priority = copy.Priority;
                this.CronExpression = copy.CronExpression;
                this.MisfireInstruction = copy.MisfireInstruction;
            }
        }

        /// <summary>
        /// Gets or sets the group of the trigger, if any.
        /// </summary>
        public string Group { get; set; }

        /// <summary>
        /// Gets or sets the name of the trigger.
        /// </summary>
        [Required]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the description of the trigger, if any.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the group of the associated job.
        /// </summary>
        [Required]
        public string JobGroup { get; set; }

        /// <summary>
        /// Gets or sets the name of the associated job.
        /// </summary>
        [Required]
        public string JobName { get; set; }

        /// <summary>
        /// Gets or sets the start time for this trigger, if any.
        /// </summary>
        [DisplayName("Start time")]
        [LowerOrEqualTo("EndTime")]
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// Gets or sets the end time for this trigger, if any.
        /// </summary>
        [DisplayName("End time")]
        [HigherOrEqualTo("StartTime")]
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// Gets or sets the priority for this trigger.
        /// </summary>
        public int Priority { get; set; }

        /// <summary>
        /// Gets or sets the cron expression that defines this trigger.
        /// </summary>
        [DisplayName("Cron expression")]
        [Required]
        [CronExpression]
        public string CronExpression { get; set; }

        /// <summary>
        /// Gets or sets the misfire instruction for this trigger.
        /// </summary>
        [DisplayName("Misfire instruction")]
        public int MisfireInstruction { get; set; }
    }
}
